package demo;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-03-18
 * Time:18:07
 */
public class TestUpdate {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/school?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        Connection connection = dataSource.getConnection();
        String sql = "update student set name = '孩子' where id = ?";
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你要修改的id:");
        int id = input.nextInt();
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        System.out.println(statement);
        System.out.println(statement.executeUpdate());
        statement.close();
        connection.close();
    }
}
